package com.lqq.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqq.eduservice.entity.EduChapter;
import com.lqq.eduservice.entity.EduVideo;
import com.lqq.eduservice.entity.chapter.ChapterVo;
import com.lqq.eduservice.entity.chapter.VideoVo;
import com.lqq.eduservice.mapper.EduChapterMapper;
import com.lqq.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lqq.eduservice.service.EduVideoService;
import com.lqq.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-29
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    //根据课程id查询章节小节
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
       //1.查询所有一级分类
        QueryWrapper<EduChapter> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id", courseId);
        List<EduChapter> chapters = baseMapper.selectList(wrapper1);

        //2.查询所有二级分类
        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id", courseId);
        List<EduVideo> videos = videoService.list(wrapper2);

        //最终返回list
        List<ChapterVo> finalList = new ArrayList<>();

        //3.封装一级分类
        for (int i = 0; i < chapters.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            EduChapter chapter = chapters.get(i);
            BeanUtils.copyProperties(chapter, chapterVo);

            //4.封装二级分类
            List<VideoVo> videoVoList = new ArrayList<>();
            for (int m = 0; m < videos.size(); m++) {
                EduVideo video = videos.get(m);
                if (video.getChapterId().equals(chapterVo.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
            //加入一级分类
            finalList.add(chapterVo);
        }
        return finalList;
    }

    //删除章节
    @Override
    public Boolean deleteChapter(String chapterId) {
        //判断章节下面是否有小节，有小结不给删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        if (count > 0) {//该章节有小节，不给删除
            throw new GuliException(20001, "该章节下有小节，删除失败");
        }
        //章节无小节,删除
        int result = baseMapper.deleteById(chapterId);

        return result > 0;
    }

    //2.根据课程id删除章节
    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
